package com.example.springbootproject.utils.Filter;/*
 *@author 王志威
 *@datatime 2024/4/13 16:44
 *@editnote
 */

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//
//@WebFilter("/*")
//@Component
public class loginFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        //通过调用接口路径来判断是不是登录
        String url = httpServletRequest.getRequestURL().toString();
        if (url.contains("/login")) {
            chain.doFilter(request, response);
            return;
        }
        if (url.contains("/register")){
            chain.doFilter(request, response);
            return;
        }
        //判断session是否为空
        if (httpServletRequest.getSession().getAttribute("user") != null) {
            //放行
            chain.doFilter(request, response);
            return;
        } else {
            //发送到登录界面
            httpServletResponse.sendRedirect("loginMain");
        }


    }
}
